//
//  DAnalysisPreferences.swift
//  Drop
//
//  Created by hushaohua on 2018/2/13.
//  Copyright © 2018年 init. All rights reserved.
//

import Foundation


class DAnalysisDatePreference{
    enum IntervalTypes:Int {
        case Day = 0
        case Week
        case Month
    }
    enum IntervalSubTypes {
        case Latest
        case Custom//peroids
        case All
    }
    enum CustomIntervalTypes{
        case beginEndDate
        case beginEndDates
        case weekDay
        case weekEnd
        case everyWeekDays
        case everyDays
    }
    
    func copy() -> DAnalysisDatePreference{
        let analysis = DAnalysisDatePreference();
        analysis.intervalType = self.intervalType;
        analysis.intervalValue.subType = self.intervalValue.subType;
        analysis.intervalValue.latestSubValue = self.intervalValue.latestSubValue;
        analysis.intervalValue.customBeginDate = self.intervalValue.customBeginDate;
        analysis.intervalValue.customEndDate = self.intervalValue.customEndDate;
        analysis.intervalValue.customDateString = self.intervalValue.customDateString;
        return analysis;
    }
    
    var intervalType:IntervalTypes = .Day;
    var intervalValue:IntervalValue;
    class IntervalValue {
        var subType:IntervalSubTypes = .Latest;
        var latestSubValue:Int?;
        var customBeginDate:Date?;
        var customEndDate:Date?;
        var customDateString:String?;
        
        init() {
            self.latestSubValue = 0;
        }
    }
    
    func analysisDescription() -> String{
        switch self.intervalType {
        case .Day:
            switch self.intervalValue.subType {
            case .All:
                return "按天";
            case .Latest:
                return self.intervalValue.latestSubValue! == 0 ? "今天" : "最近\(self.intervalValue.latestSubValue! + 1)天"
            case .Custom:
                return self.intervalValue.customDateString!;
            }
        case .Week:
            switch self.intervalValue.subType {
            case .All:
                return "按周";
            case .Latest:
                return self.intervalValue.latestSubValue! == 0 ? "本周" : "最近\(self.intervalValue.latestSubValue! + 1)周"
            case .Custom:
                return self.intervalValue.customDateString!;
            }
        case .Month:
            switch self.intervalValue.subType {
            case .All:
                return "按月";
            case .Latest:
                return self.intervalValue.latestSubValue! == 0 ? "本月" : "最近\(self.intervalValue.latestSubValue! + 1)月"
            case .Custom:
                return self.intervalValue.customDateString!;
            }
        }
    }
    
    func queryBeginDate() -> Date?{
        switch self.intervalValue.subType {
        case .Latest:
            return self.latestBeginDate();
        case .Custom:
            return self.intervalValue.customBeginDate;
        case .All:
            return nil;
        }
    }
    
    func queryEndDate() -> Date?{
        switch self.intervalValue.subType {
        case .Latest:
            return self.latestEndDate();
        case .Custom:
            return self.intervalValue.customEndDate;
        case .All:
            return nil;
        }
    }
    
    func latestBeginDate() -> Date?{
        if (self.intervalValue.subType == .Latest){
            switch self.intervalType {
            case .Day:
                    return Date().startDayDate(after:-self.intervalValue.latestSubValue!);
            case .Week:
                    return Date().firstWeekDay(after: -self.intervalValue.latestSubValue!);
            case .Month:
                    return Date().firstMonthDay(after: -self.intervalValue.latestSubValue!);
            }
        }
        return nil;
    }
    
    func latestEndDate() -> Date?{
        if (self.intervalValue.subType == .Latest){
            return Date();
        }
        return nil;
    }
    
    init() {
        self.intervalValue = IntervalValue();
        
    }
}
